// pages/repair-detail/repair-detail.js
import api from "../../../utils/request"
Page({
    data: {
      id: "",
      statusClass: "pending",
      detail: {
        id: "1",
        repairNumber: "WX202312010001",
        repairTime: "2023-12-01",
        repairTypeName: "水电维修",
        typeTag: "primary",
        repairArea: "厨房",
        problemDescription: "厨房水龙头漏水，需要更换密封圈或水龙头。水流较大，已经用毛巾临时包裹，希望尽快处理。",
        status: 1,
        statusText: "待接单",
        houseAddress: "城市花园1栋1单元1层101",
        staffName: "王师傅",
        lastAcceptTime: "2023-12-02",
      },
      steps: [
        {
          text: "待派单",
          desc: "报修已提交",
        },
        {
          text: "待接单",
          desc: "维修人员已分配",
        },
        {
          text: "已完成",
          desc: "维修已完成",
        },
      ],
    },
  
    onLoad(options) {
      if (options.id) {
        this.setData({
          id: options.id,
        })
        this.fetchDetail(options.id)
      }
    },
  
    // 获取详情数据
  fetchDetail(id) {
    wx.showLoading({
      title: "加载中...",
    });

    // 模拟API请求
    setTimeout(async () => {
      try {
        // 实际项目中这里应该是API请求
        const result = await api.get(`/property/repairRequest/${id}`);
        
        if (result.code === 200) {
          const detailData = result.data;
          const processedDetail = {
            id: detailData.id,
            repairNumber: detailData.repairNumber,
            repairTime: detailData.repairTime,
            repairTypeName: detailData.repairTypeName,
            typeTag: "success", // 根据报修类型设置 typeTag
            repairArea: detailData.repairArea,
            problemDescription: detailData.problemDescription,
            status: detailData.status,
            statusText: this.getStatusText(detailData.status), // 根据状态设置 statusText
            houseAddress: detailData.houseAddress,
            staffName: detailData.staffName,
            lastAcceptTime: detailData.lastAcceptTime || "未分配", // 如果没有分配时间，默认显示 "未分配"
          };

          this.setData({
            detail: processedDetail,
            statusClass: this.getStatusClass(detailData.status),
          });
        } else {
          wx.showToast({
            title: result.msg || "加载失败",
            icon: "none",
          });
        }
      } catch (error) {
        console.error("加载报修详情失败", error);
        wx.showToast({
          title: "加载失败，请稍后再试",
          icon: "none",
        });
      }

      wx.hideLoading();
    }, 500);
  },

  // 获取状态文本
  getStatusText(status) {
    const statusTextMap = {
      0: "待派单",
      1: "待接单",
      2: "已完成",
    };
    return statusTextMap[status] || "未知状态";
  },
  
    // 获取状态样式类
    getStatusClass(status) {
      const statusMap = {
        0: "pending",
        1: "processing",
        2: "completed",
      }
      return statusMap[status] || "pending"
    },
  
    // 评价维修
    onEvaluate() {
      wx.navigateTo({
        url: `/pages/repair-evaluate/repair-evaluate?id=${this.data.id}`,
      })
    },
  })
  